package com.bsh.repository;

import java.math.BigDecimal;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.bsh.utils.JdbcHelper;

/**
 * 统计
 * @author gsh
 *
 */
@Repository
public class DaoAccountState{
	@Resource
	SqlSessionTemplate session;
	@Resource
	JdbcTemplate jdbcTemplate;
	@Resource
	NamedParameterJdbcTemplate namedJdbcTemplate;
	@Autowired
	DaoAccountNotice daoNotice;	
	@Autowired
	JdbcHelper jdbc;

	/**
	 * 后台充值(元)", "0.00"
	 * @return
	 */
	public BigDecimal getRechargeByAdmin(String where){
		StringBuilder sb=new StringBuilder();
		sb.append("select sum(money) money from account_notice ");
		sb.append(" where trade_type=7 and money>0 and is_paid=1 ");
		if(!StringUtils.isEmpty(where))sb.append(where);
		BigDecimal res = jdbc.scalarBigDecimal(sb.toString(),null);
		return res;
	}
	
	/**
	 * 后台扣除(元)", "0.00"
	 * @return
	 */
	public BigDecimal getDeductionByAdmin(String where){
		StringBuilder sb=new StringBuilder();
		sb.append("select sum(money) money from account_notice ");
		sb.append(" where trade_type=7 and money<0 and is_paid=1 ");
		if(!StringUtils.isEmpty(where))sb.append(where);
		BigDecimal res = jdbc.scalarBigDecimal(sb.toString(),null);
		return res;
	}
	
	/**
	 * 用户在线充值(元)", "0=0.00(支付宝)+0.00(微信)"
	 * @return
	 */
	public BigDecimal[] getRechargeOnline(String where){
		StringBuilder sb=new StringBuilder();
		sb.append("select sum(money) money from account_notice ");
		sb.append(" where trade_type=1 and money>0 and is_paid=1 and payment_id in(29,30,31)");
		if(!StringUtils.isEmpty(where))sb.append(where);
		BigDecimal res = jdbc.scalarBigDecimal(sb.toString(),null);
		
		StringBuilder sb2=new StringBuilder();
		sb2.append("select sum(money) money from account_notice ");
		sb2.append(" where trade_type=1 and money>0 and is_paid=1 and payment_id in(32)");
		if(!StringUtils.isEmpty(where))sb2.append(where);
		BigDecimal res2 = jdbc.scalarBigDecimal(sb2.toString(),null);
		
		BigDecimal[] ary=new BigDecimal[2];
		ary[0]=res;
		ary[1]=res2;
		return ary;
	}
	
	/**
	 * 用户提现(元)", "0.00"
	 * @return
	 */
	public BigDecimal getCash(String where){
		StringBuilder sb=new StringBuilder();
		sb.append("select sum(money) money from account_notice ");
		sb.append(" where trade_type=3 and money<0 and is_paid=1 ");
		if(!StringUtils.isEmpty(where))sb.append(where);
		BigDecimal res = jdbc.scalarBigDecimal(sb.toString(),null);
		return res;
	}
	

	/**
	 * 余额转账转入(元)", "162300.00"
	 * @return
	 */
	public BigDecimal getTranferIn(String where){
		return BigDecimal.ZERO;
//		StringBuilder sb=new StringBuilder();
//		sb.append("select sum(money) money from account_notice ");
//		sb.append(" where trade_type=2 and money>0 and is_paid=1 ");
//		if(!StringUtils.isEmpty(where))sb.append(where);
//		BigDecimal res = jdbc.scalarBigDecimal(sb.toString(),null);
//		return res;
	}

	/**
	 * 余额转账转出(元)", "137955.00"
	 * @return
	 */
	public BigDecimal getTranferOut(String where){
		return BigDecimal.ZERO;
//		StringBuilder sb=new StringBuilder();
//		sb.append("select sum(money) money from account_notice ");
//		sb.append(" where trade_type=2 and money<0 and is_paid=1 ");
//		if(!StringUtils.isEmpty(where))sb.append(where);
//		BigDecimal res = jdbc.scalarBigDecimal(sb.toString(),null);
//		return res;
	}
	
	/**
	 * 在线转账转入(元)", "4504=0.00(支付宝)+4504.00(微信)"
	 * @return
	 */
	public BigDecimal getTranferInOnline(String where){
		return null;
	}
	
	/**
	 * 在线转账转出(元)", "3828.4=0.00(支付宝)+3828.40(微信)"
	 * @return
	 */
	public BigDecimal  getTranferOutOnline(String where){
		return null;
	}
	
	/**
	 * 系统昨日返现(元)", "0.00"
	 * @return
	 */
	public BigDecimal getRefundMony(String where){
		StringBuilder sb=new StringBuilder();
		sb.append("select sum(money) money from account_notice ");
		sb.append(" where trade_type=9 and money>0 and is_paid=1 ");
		if(!StringUtils.isEmpty(where))sb.append(where);
		BigDecimal res = jdbc.scalarBigDecimal(sb.toString(),null);
		return res;
	}
	
	/**
	 * 推荐人返利，现金(元)", "0=0.00(余额)+0.00(支付宝)+0.00(微信)"
	 * ！！！！统计结果为0，原因，目前只返商币,payment_id=1 and cloud_money>0
	 * @return
	 */
	public BigDecimal[] getReferralRefund(String where){
		StringBuilder sb=new StringBuilder();
		sb.append("select sum(money) money from account_notice ");
		sb.append(" where trade_type=6 and money>0 and is_paid=1 and payment_id in(29,30,31)");
		if(!StringUtils.isEmpty(where))sb.append(where);
		BigDecimal res = jdbc.scalarBigDecimal(sb.toString(),null);

		StringBuilder sb2=new StringBuilder();
		sb2.append("select sum(money) money from account_notice ");
		sb2.append(" where trade_type=6 and money>0 and is_paid=1 and payment_id=32");
		if(!StringUtils.isEmpty(where))sb2.append(where);
		BigDecimal res2 = jdbc.scalarBigDecimal(sb2.toString(),null);
		
		BigDecimal[] ary=new BigDecimal[2];
		ary[0]=res;
		ary[1]=res2;
		return ary;
	}

	/**
	 * 余额升级VIP(元)", "0.00"
	 * @return
	 */
	public BigDecimal getUpgradeVipByBalance(String where){
		StringBuilder sb=new StringBuilder();
		sb.append("select sum(money) money from account_notice ");
		sb.append(" where trade_type=4 and money=-100 and is_paid=1 and payment_id=4");
		if(!StringUtils.isEmpty(where))sb.append(where);
		BigDecimal res = jdbc.scalarBigDecimal(sb.toString(),null);
		return res;
	}
	
	/**
	 * 在线支付--升级VIP(元)", "0=0.00(支付宝)+0.00(微信)"
	 * @return
	 */
	public BigDecimal[] getUpgradeVipOnline(String where){
		StringBuilder sb=new StringBuilder();
		sb.append("select sum(money) money from account_notice ");
		sb.append(" where trade_type=4 and money=100 and is_paid=1 and payment_id in(29,30,31)");
		if(!StringUtils.isEmpty(where))sb.append(where);
		BigDecimal res = jdbc.scalarBigDecimal(sb.toString(),null);

		StringBuilder sb2=new StringBuilder();
		sb2.append("select sum(money) money from account_notice ");
		sb2.append(" where trade_type=4 and money=100 and is_paid=1 and payment_id=32");
		if(!StringUtils.isEmpty(where))sb2.append(where);
		BigDecimal res2 = jdbc.scalarBigDecimal(sb2.toString(),null);
		
		BigDecimal[] ary=new BigDecimal[2];
		ary[0]=res;
		ary[1]=res2;
		return ary;
	}
	
	/**
	 * 余额升级金钻(元)", "1000.00"
	 * @return
	 */
	public BigDecimal getUpgradeGoldByBalance(String where){
		return BigDecimal.ZERO;
//		StringBuilder sb=new StringBuilder();
//		sb.append("select sum(money) money from account_notice ");
//		sb.append(" where trade_type=15 and money=1000 and is_paid=1 ");
//		if(!StringUtils.isEmpty(where))sb.append(where);
//		BigDecimal res = jdbc.scalarBigDecimal(sb.toString(),null);
//		return res;
	}	
	
	/**
	 * 在线支付--升级金钻(元)", "0=0.00(支付宝)+0.00(微信)"
	 * @return
	 */
	public BigDecimal[] getUpgradeGoldOnline(String where){
		StringBuilder sb=new StringBuilder();
		sb.append("select sum(money) money from account_notice ");
		sb.append(" where trade_type=4 and money=1000 and is_paid=1 and payment_id in(29,30,31)");
		if(!StringUtils.isEmpty(where))sb.append(where);
		BigDecimal res = jdbc.scalarBigDecimal(sb.toString(),null);

		StringBuilder sb2=new StringBuilder();
		sb2.append("select sum(money) money from account_notice ");
		sb2.append(" where trade_type=4 and money=1000 and is_paid=1 and payment_id=32");
		if(!StringUtils.isEmpty(where))sb2.append(where);
		BigDecimal res2 = jdbc.scalarBigDecimal(sb2.toString(),null);
		
		BigDecimal[] ary=new BigDecimal[2];
		ary[0]=res;
		ary[1]=res2;
		return ary;
	}	
	
	

	/**
	 * 后台充值商币(元)", "0.00"
	 * @return
	 */
	public BigDecimal getRechargeCloudByAdmin(String where){
		StringBuilder sb=new StringBuilder();
		sb.append("select sum(cloud_money) cloud_money from account_notice ");
		sb.append(" where trade_type=8 and cloud_money>0 and is_paid=1 ");
		if(!StringUtils.isEmpty(where))sb.append(where);
		BigDecimal res = jdbc.scalarBigDecimal(sb.toString(),null);
		return res;
	}
	
	/**
	 * 后台扣除商币(元)", "0.00"
	 * @return
	 */
	public BigDecimal getDeductionCloudByAdmin(String where){
		StringBuilder sb=new StringBuilder();
		sb.append("select sum(cloud_money) cloud_money from account_notice ");
		sb.append(" where trade_type=8 and cloud_money<0 and is_paid=1 ");
		if(!StringUtils.isEmpty(where))sb.append(where);
		BigDecimal res = jdbc.scalarBigDecimal(sb.toString(),null);
		return res;
	}
	
	
	/**
	 * 现金合计(元)", "0.00"
	 * @return
	 */
	public BigDecimal getMoneySum(){
		StringBuilder sb=new StringBuilder();
		sb.append("select sum(money) cloud_money from account ");
		//sb.append(" where trade_type=7 and cloud_money>0 and is_paid=1 ");
		//if(!StringUtils.isEmpty(where))sb.append(where);
		BigDecimal res = jdbc.scalarBigDecimal(sb.toString(),null);
		return res;
	}
	
	/**
	 * 商币合计(元)", "0.00"
	 * @return
	 */
	public BigDecimal getCloudSum(){
		StringBuilder sb=new StringBuilder();
		sb.append("select sum(cloud_money) cloud_money from account ");
		//sb.append(" where trade_type=7 and cloud_money<0 and is_paid=1 ");
		//if(!StringUtils.isEmpty(where))sb.append(where);
		BigDecimal res = jdbc.scalarBigDecimal(sb.toString(),null);
		return res;
	}
}
